<?php
class ControllerModuleSendQuestion extends Controller {
    private $error = array();
    
    public function index() {    
        //$this->document->title = 'Send et spørgsmål til dette produkt';
        $this->data['template'] = $this->config->get('config_template');  

        $this->data['action'] = HTTPS_SERVER . 'index.php?route=module/send_question';        
        $this->data['send'] = HTTPS_SERVER . 'index.php?route=module/send_question/sendQuestion';    
        $this->data['telephone'] = $this->config->get('config_telephone'); 
        $this->data['phone_hours'] = '11-16';
        
        $this->id = 'send_question';
        
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/send_question.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/send_question.tpl';
        } else {
            $this->template = 'default/template/module/send_question.tpl';
        }
        
        $this->render();
    }
      
    public function sendQuestion() {            
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()){
            $result = 'Dit spørgsmål er sendt!';
            
            //$subject = 'Tip Event';
            $subject = 'Question for product';
            
            $message = 'A customer has been sent to you an email to ask for a product. Below is details:<br /><br />';
            $message .= 'Name:&nbsp;&nbsp;<span><b>' . $this->request->post['client_name'] . '</b></span><br />';
            $message .= 'Phone:&nbsp;&nbsp;<span><b>' . $this->request->post['client_phone'] . '</b></span><br />';
            $message .= 'Email:&nbsp;&nbsp;<span><b>' . $this->request->post['client_email'] . '</b></span><br />';
            $message .= 'Comment:&nbsp;&nbsp;<span><b>' . $this->request->post['client_comment'] . '</b></span><br /><br />';
            $message .= 'Product link:&nbsp;&nbsp;<a href="' . $this->request->post['product_link'] . '"><span><b>' . $this->request->post['product_link'] . '</b></span></a><br />';

            $receiver = $this->config->get('config_email'); //'mwc.testing@gmail.com';//
            //$this->sendMail($this->request->post['client_email'], $receiver, $subject, $message);
            $this->sendMailMwcMailer($this->request->post['client_email'], $this->request->post['client_name'], $receiver, $subject, $message);            
        }
        else{
            if(!empty($this->error)){
                reset($this->error);
                $result = current($this->error);
            }
            
            $result = 'Ukendt fejl';
        }
        
        echo $result;            
    }
      
    private function validate() {
        $pattern = '/^[A-Z0-9._%-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i';
        $error = false;
    
        if ($this->request->post['client_name'] == '') {
              $this->error['client_name'] = 'Ugyldigt navn';
              $error = true;
        }

        if (!preg_match($pattern, $this->request->post['client_email'])) {
              $this->error['client_email'] = 'Indtast venligst en gyldig e-mail';
              $error = true;
        }
             
        if (!$error) {     
              return true;     
        } 
        else {
              return false;
        }
    }
      
    private function sendMailMwcMailer($from, $from_name, $to, $subject, $message){        
        require_once(DIR_MAILER . 'mwc_mailer.php');
        
        $mail = new MwcMailer(); 
        if($this->config->get('config_mail_protocol') == 'smtp'){           
            $mail->Host = $this->config->get('config_smtp_host');
            $mail->Username = $this->config->get('config_smtp_username');
            $mail->Password = $this->config->get('config_smtp_password');
            $mail->Port = $this->config->get('config_smtp_port');            
        
            $mail->IsSMTP();
        }
                
        $mail->setFrom($from, $from_name);                     
        $mail->Subject = $subject;
        $mail->MsgHTML($message);
        $mail->AddAddress($to); 
       
        
        $mail->send();            
    }
    
    private function sendMail($from, $to, $subject, $message){
        $mail = new Mail();
        $mail->protocol = $this->config->get('config_mail_protocol');
        $mail->parameter = $this->config->get('config_mail_parameter');
        $mail->hostname = $this->config->get('config_smtp_host');
        $mail->username = $this->config->get('config_smtp_username');
        $mail->password = $this->config->get('config_smtp_password');
        $mail->port = $this->config->get('config_smtp_port');
        $mail->timeout = $this->config->get('config_smtp_timeout');        
        $mail->setFrom($from);
        $mail->setTo($to);
        $mail->setSender($from);
        $mail->setSubject($subject);
        $mail->setHtml($message); 
        $mail->send();    
    } 
}
?>                   